package com.mhy.anno;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class PoolsMain {

	public static void main(String[] args) throws Exception {
		ApplicationContext ac = new ClassPathXmlApplicationContext("dbpools.xml");

		//DruidDataSource connectionPools = ac.getBean("dataSource2", DruidDataSource.class);
		DataSource dataSource = ac.getBean("dataSource2", DataSource.class);
		System.out.println(dataSource.getConnection());
		//DruidPooledConnection conn = connectionPools.getConnection();
		Connection conn = dataSource.getConnection();
		// 3. 获取Connection
		Statement stmt = conn.createStatement();
		// 4.执行SQL语句并且遍历查询结果
		String sql = "select * from user";
		ResultSet rs = stmt.executeQuery(sql);
		while (rs.next()) {
			System.out.println(rs.getInt("id") + " " + rs.getString("username"));
		}
		// 5.关闭资源
		rs.close();
		stmt.close();
		conn.close();
	}

}
